package com.jichangxiu.framework.utils;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Getter;

import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class AsyncUtils {

    // 操作延迟 10 毫秒
    public static final int OPERATE_DELAY_TIME = 10;

    @Getter
    private static final AsyncUtils INSTANCE = new AsyncUtils();

    private final ScheduledExecutorService executor = SpringUtil.getBean("scheduledExecutorService");

    private AsyncUtils() {
    }

    public void execute(TimerTask task) {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    public void shutdown() {
        ThreadUtils.shutdownAndAwaitTermination(executor);
    }

}
